package com.yihu.cms.action;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.article.beans.ResultPage;
import com.yihu.beans.ResultVO;
import com.yihu.beans.user.Group;
import com.yihu.beans.user.GroupQuery;
import com.yihu.beans.user.GroupRoleRef;
import com.yihu.beans.user.Role;
import com.yihu.beans.user.RolePermitRef;
import com.yihu.beans.user.RoleQuery;
import com.yihu.beans.user.User;
import com.yihu.beans.user.UserGroupRef;
import com.yihu.cms.service.GroupRoleService;
import com.yihu.cms.service.UserGroupService;

@Controller
public class GroupRoleController extends BaseController{
	

	private GroupRoleService us = new GroupRoleService();
	@RequestMapping(value = "/user/role" , method = RequestMethod.POST)
	public @ResponseBody Object addRole(@RequestBody Role role) {
		log.debug("addRole: "+role.toString());
		ResultVO result= us.addRole(role);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/role/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updateRole(@RequestBody Role role,@PathVariable("id") long id) {
		role.setRoleId(id);
		log.debug("updateRole: "+role.toString());
		ResultVO result= us.updateRole(role);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/role/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deleteRole(@PathVariable("id") long id) {
		log.debug("deleteRole: "+id);
		ResultVO result= us.deleteRole(id);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/role/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getRole(@PathVariable("id") long id) {
		log.debug("getRole id: "+id);
		Role role = us.getRole(id);
		log.debug("getRole： "+role.toString());
		return JSON.parseObject(JSON.toJSONString(role), Role.class);
	}
	
	@RequestMapping(value = "/user/role/list" , method = RequestMethod.GET)
	// , method = RequestMethod.PATCH
	public @ResponseBody Object getRolesPaging(RoleQuery query) {
		log.debug("getRolesPaging "+query.toString());
		String resultJson= us.getRoleByPage(query);
		return JSON.parse(resultJson);
	}
	
	@RequestMapping(value = "/user/role/groups", method = RequestMethod.POST)
	public @ResponseBody Object getRoleGroups(@RequestBody RoleQuery query) {
		log.debug("getGroupUsers : "+query);
		Role role = us.getGroupsByRoleId(query);
		return JSON.parse(JSON.toJSONString(role));
	}
	
	@RequestMapping(value = "/user/role/addGroup", method = RequestMethod.POST)
	public @ResponseBody Object addGroupToRole(@RequestBody GroupRoleRef gRef) {
		log.debug("addUserToGroup : "+gRef);
		ResultVO r = us.addGroupToRole(gRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/role/deleteGroup", method = RequestMethod.POST)
	public @ResponseBody Object deleteGroupToRole(@RequestBody GroupRoleRef gRef) {
		log.debug("deleteGroupToRole : "+gRef);
		ResultVO r = us.deleteGroupToRole(gRef);
		return JSON.toJSONString(r);
	}
	
	
	@RequestMapping(value = "/user/role/addPriviledge", method = RequestMethod.POST)
	public @ResponseBody Object addPriviledgeToRole(@RequestBody RolePermitRef rRef) {
		log.debug("addPriviledgeToRole : "+rRef);
		ResultVO r = us.addPriviledgeToRole(rRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/role/deletePriviledge", method = RequestMethod.POST)
	public @ResponseBody Object deletePriviledge(@RequestBody RolePermitRef rRef) {
		log.debug("addPriviledgeToRole : "+rRef);
		ResultVO r = us.deletePriviledgeFromRole(rRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/role/priviledges", method = RequestMethod.POST)
	public @ResponseBody Object getPriviledge(@RequestBody RoleQuery query) {
		log.debug("getPriviledge : "+query);
		Object o= us.getPriviledgesByRoleId(query);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	@RequestMapping(value = "/user/role/assignPermits", method = RequestMethod.POST)
	public @ResponseBody Object assignPermits(@RequestBody RolePermitRef rRef) {
		log.debug("assignGroups : "+rRef);
		ResultVO r = us.assignPermits(rRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/role/assignGroups", method = RequestMethod.POST)
	public @ResponseBody Object assignGroups(@RequestBody GroupRoleRef gRef) {
		log.debug("assignGroups : "+gRef);
		ResultVO r = us.assignGroups(gRef);
		return JSON.toJSONString(r);
	}
}
